package com.sms.web.domain.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sms.domain.entity.Template;
import com.sms.domain.service.TemplateService;
import com.sms.web.rest.RestObject;

/**
 * @desc 模板管理
 * @author wjw
 * @time 2017年11月21日上午10:45:11
 */
@Controller
@RequestMapping(value = "/admin/template")
public class TemplateController {
    
    protected static Logger logger = LoggerFactory.getLogger(TemplateController.class);
    
    /** template服务 */
    @Autowired
    private TemplateService templateService;
    
    /**
     * 配置信息列表
     * @param modelMap 模型
     * @return 视图
     */
    @RequestMapping(value = "")
    public String configList(ModelMap modelMap) {
        return "template/list";
    }
    
    /**
     * 配置信息列表分页
     * @param queryLike 查询条件
     * @param pageNumber 页数
     * @param pageSize 条数
     * @param model model
     * @return String
     */
    @RequestMapping(value = "/page")
    public String pageList(String queryLike, @RequestParam(defaultValue = "1") int pageNumber, 
    		@RequestParam(defaultValue = "20") int pageSize,
        ModelMap model) {
        Map<String, Object> filter = new HashMap<>();
        int page = pageNumber < 0 ? 1 : pageNumber;
        Page<Template> list = null;
        if (StringUtils.isNotBlank(queryLike)) {
            filter.put("LIKE_name", queryLike);
        }
        list = templateService.findAll(filter, page, pageSize);
        model.put("data", list);
        return "template/_page";
    }
    
    /**
     * 详细信息
     * @param templateId 应用id
     * @param model model
     * @return 视图
     */
    @RequestMapping(value = "/detail")
    public String userDetail(String templateId, ModelMap model) {
        if (StringUtils.isNotBlank(templateId)) {
            model.put("template", templateService.getOne(templateId));
        }
        return "template/form";
    }
    
    /**
     * 保存模板
     * @param template
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public RestObject save(Template template) {
        Template newTemplate = null;
        if (StringUtils.isBlank(template.getCode())) {
            return RestObject.newError("code不能为空");
        }
        try {
            // 同一个应用下模板编码不能相同
            newTemplate = templateService.findByAppAndCode(template.getApplication().getAppId(), template.getCode());
            if (newTemplate != null && !StringUtils.equals(newTemplate.getTemplateId(), template.getTemplateId())) {
                return RestObject.newError("该应用下,code:" + template.getCode() + "已经存在");
            }
            newTemplate = templateService.save(template);
        } catch (Exception ex) {
            logger.error("在保存模板信息时异常", ex);
            return RestObject.newError(ex.getMessage());
        }
        return RestObject.newOk(newTemplate);
    }
    
    /**
     * 删除应用
     * @param templateId xx
     * @return 视图
     */
    @RequestMapping(value = "/del")
    @ResponseBody
    public RestObject del(String templateId) {
        try {
            if (StringUtils.isNotEmpty(templateId)) {
                templateService.delete(templateId);
            }
        } catch (Exception ex) {
            logger.error("在删除模板信息时异常", ex);
            return RestObject.newError(ex.getMessage());
        }
        return RestObject.newOk("success");
    }
    
}
